﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnalisisDeRiesgos.Models.Action;
using AnalisisDeRiesgos.Models;
using Microsoft.Web.Mvc;

namespace AnalisisDeRiesgos.Controllers
{
    public class ActionController : Controller
    {
        //
        // GET: /Action/
        [AcceptAjax]
        public PartialViewResult Index(int ?id)
        {
            if ( id == null )
                return PartialView("ErrorPartial", new ErrorModel { Message = "El id no puede ser vacio" });
            IndexActionModel model = new IndexActionModel(id.Value);

            return PartialView(model);
        }

        //
        // GET: /Action/Details/5
        [AcceptAjax]
        public PartialViewResult Details(int ?id)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.AreaSecurity))
                return PartialView("ErrorPartial", new ErrorModel { Message = "No tienes los persmisos suficientes para hacer esto" });
            
            try
            {
                DetailsActionModel model = new DetailsActionModel(id.Value);

                return PartialView(model);
            }
            catch (Exception ex)
            {
                return PartialView("ErrorPartial", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // GET: /Action/Create
        [AcceptAjax]
        public PartialViewResult Create(int ?id, RegisterActionModel model)
        {
            if ( id == null )
                return PartialView("ErrorPartial", new ErrorModel{ Message = "Error, el id no puede ser nulo"});

            try
            {
                if (model.DangerId == 0)
                {
                    model = new RegisterActionModel(id.Value);
                    return PartialView("Create", model);
                }

                if (!ModelState.IsValid)
                {
                    return PartialView("Create", model);
                }

                model.Save();
                return PartialView("Create", model);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return PartialView(model);
            }
        } 
        
        //
        // GET: /Action/Edit/5
        [AcceptAjax]
        public PartialViewResult Edit(int ?id, EditActionModel model)
        {
            if (id == null)
                return PartialView("ErrorPartial", new ErrorModel { Message = "Error, el id no puede ser nulo" });

            try
            {
                if (model.ActionId == 0 )
                    model = new EditActionModel(id.Value);
                if (!ModelState.IsValid)
                    return PartialView("Edit", model);

                model.Save();               

                return PartialView("Edit", model);
            }
            catch (Exception ex)
            {
                return PartialView("ErrorPartial", new ErrorModel { Message = ex.Message });
            }
        }

                //
        // GET: /Action/Delete/5
        [AcceptAjax]
        public PartialViewResult Delete(int ?id, DetailsActionModel model)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.GeneralSecurity))
                return PartialView("ErrorPartial", new ErrorModel { Message = "Error, usted no puede eliminar esta accion" });

            if (id == null)
                return PartialView("ErrorPartial", new ErrorModel { Message = "Error, el id no puede ser nulo" });
            try
            {
                if (model.ActionId == 0)
                {
                    model = new DetailsActionModel(id.Value);
                    return PartialView("Delete", model);
                }
                
                try
                {
                    model.DeleteAction();
                    return PartialView("Delete", model);
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }

                return PartialView("Delete", model);
            }
            catch (Exception ex)
            {
                return PartialView("ErrorPartial", new ErrorModel { Message = ex.Message });
            }
        }        
    }
}
